<template>
  <div class="admin-adminer">
    <div class="top">
      <div class="adminer-search">
        <el-input placeholder="请输入用户名..." v-model="searchInput">
          <template slot="append">
            <el-button>搜索</el-button>
          </template>
        </el-input>
      </div>
      <div class="add">
        <el-button type="primary" @click="dialogVisible = true">添加管理员</el-button>
      </div>
    </div>

    <div class="adminer-table">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="index" label="序号">
        </el-table-column>
        <el-table-column prop="id" label="管理员ID">
        </el-table-column>
        <el-table-column prop="adminName" label="账号">
        </el-table-column>
        <el-table-column prop="admin_nickname" label="管理员昵称">
        </el-table-column>
        <el-table-column prop="status" label="状态">
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button @click="handleDelete(scope.row.id)" type="text" size="small"
              :disabled="scope.row.id === adminInfo.adminId">
              <span style="color: red;">删除</span>
            </el-button>
            <el-button type="text" size="small" @click="edit(scope.row.id)"
              :disabled="scope.row.id === adminInfo.adminId || scope.row.id === 1">操作</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="dialog">
      <el-dialog title="添加管理员" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <div class="form">
          <el-form ref="form" :model="form" :rules="rules">
            <el-form-item label="账号：" prop="adminName">
              <el-input v-model="form.adminName"></el-input>
            </el-form-item>
            <el-form-item label="管理员姓名：" prop="admin_nickname">
              <el-input v-model="form.admin_nickname"></el-input>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
          <el-button type="primary" @click="handleAdd" size="mini">添 加</el-button>
        </span>
      </el-dialog>
    </div>
    <div class="edit_dialog">
      <el-dialog title="设置状态" :visible.sync="editDialog" width="30%" :before-close="EditClose">
        <div class="editForm">
          <el-form ref="editForm" :model="editForm" :rules="editRules">
            <el-form-item label="修改状态：" prop="status">
              <el-select v-model="editForm.status" placeholder="请选择">
                <el-option label="正常" value="0"></el-option>
                <el-option label="禁用" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialog = false" size="mini">取 消</el-button>
          <el-button type="primary" @click="handleEdit" size="mini">修 改</el-button>
          <el-button type="danger" @click="handleReject()" size="mini">重置密码</el-button>
          <p><i>*重置密码为‘123456’，请谨慎操作！</i></p>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAllAdminInfoService, updateAdminService, addAdminService, resetPwdService } from '@/api/admin'
import { Message } from 'element-ui'
import { mapState } from 'vuex'
export default {
  data () {
    return {
      slectId: '',
      searchInput: '',
      tableData: [],
      dialogVisible: false,
      editDialog: false,
      form: {
        adminName: '',
        admin_nickname: ''
      },
      rules: {
        adminName: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { pattern: /^[/\S]{4,8}$/, message: '长度在 4 到 8 个非空字符', trigger: 'blur' }
        ],
        admin_nickname: [
          { required: true, message: '请输入管理员姓名', trigger: 'blur' }
        ]
      },
      editForm: {
        status: ''
      },
      editRules: {
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ]
      }

    }
  },
  created () {
    this.getAllAdminInfo()
  },
  methods: {
    async getAllAdminInfo () {
      const res = await getAllAdminInfoService()
      console.log(this.adminInfo)
      res.data.forEach((item, index, arr) => {
        arr[index].index = index + 1
        arr[index].status = item.status === '0' ? '正常' : '禁用'
      })
      this.tableData = res.data
    },
    handleClose (done) {
      done()
    },
    EditClose (done) {
      done()
    },
    async handleAdd () {
      await this.$refs.form.validate()
      const res = await addAdminService(this.form)
      if (res.status === 0) {
        this.dialogVisible = false
        await Message.success(res.message)
        this.getAllAdminInfo()
      }
      this.form = {}
    },
    handleDelete (id) {
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // TODO: 调用接口
        const res = await updateAdminService(id, '2', '3')
        if (res.status === 0) {
          await this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
          await this.$message({
            type: 'error',
            message: res.message
          })
        }
        this.getAllAdminInfo()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    edit (id) {
      this.slectId = id
      this.editDialog = true
    },
    async handleEdit () {
      await this.$refs.editForm.validate()
      this.$confirm('确认修改吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // TODO: 调用接口
        const res = await updateAdminService(this.slectId, this.editForm.status, '3')
        if (res.status === 0) {
          await this.$message({
            type: 'success',
            message: '修改成功!'
          })
        } else {
          await this.$message({
            type: 'error',
            message: res.message
          })
        }
        this.slectId = ''
        this.editForm = {}
        this.getAllAdminInfo()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        })
      })
      this.editDialog = false
    },
    async handleReject () {
      await resetPwdService(this.slectId, '2').then(res => {
        if (res.status === 0) {
          this.$message({
            message: '重置密码成功',
            type: 'success'
          })
          this.editDialog = false
        } else {
          this.$message({
            message: '重置密码失败',
            type: 'error'
          })
        }
      })
    }
  },
  computed: {
    ...mapState('adminStore', ['adminInfo'])
  }

}
</script>

<style lang="less" scoped>
.admin-adminer {
  width: 100%;
  height: 100%;
  background: #fff;

  .top{
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    .adminer-search {
        padding: 20px 20px 0px 20px;
        width: 50%;
      }
      .add{
        flex: 1;
      }

  }

  .adminer-table {
    margin-top: 20px;
    padding: 20px;
    width: 100%;
  }
  i{
    color: #ccc;
    font-size: 10px;
  }
}
</style>
